package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _4删除有序数组中的重复项II
 * @description
 * @date 2023/07/06 9:39
 * <p>
 * ******************************************************
 */
public class _4删除有序数组中的重复项II {

    public int removeDuplicates(int[] nums) {
        int times = 1;
        int slow = 0;
        int fast = 1;

        while (fast < nums.length) {
            if (nums[slow] == nums[fast]) {
                if (times < 2) {
                    // 存在值就得换位置
                    if (fast - slow > 1) {
                        int temp = nums[fast];
                        nums[fast] = nums[slow + 1];
                        nums[slow + 1] = temp;
                    }
                    times++;
                    slow++;
                }
            } else {
                // 存在值就得换位置
                if (fast - slow > 1) {
                    int temp = nums[fast];
                    nums[fast] = nums[slow + 1];
                    nums[slow + 1] = temp;
                }
                slow++;

                times = 1;
            }
            fast++;
        }
        return slow + 1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{0,0,1,1,1,1,2,3,3};
        _4删除有序数组中的重复项II s = new _4删除有序数组中的重复项II();
        s.removeDuplicates(nums);
        System.out.println();

    }

}
